/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-02-26 15:58:39
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-21 11:20:24
 */
// pages/goods/index.js
const app = getApp()

import {
  returnStatus
} from '../../../utils/util'
import { globalAPI } from "../../../api/index.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    requestObj: {},
    records:[],
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
    },
  },
  toSearch(){
    wx.redirectTo({
      url: '/pages/search/index',
    })
  },
  // 获取订单数据
  getData(orderStatus,pageSize=10) {
    this.setData({
      loadFinish: false
    })
    return new Promise(async (resolve, reject) => {
      const res = await globalAPI.queryGoodsByKey({
        pageNum: this.data.pageNum,
        keyword: this.data.key,
        pageSize
      })
      const {records} = await returnStatus(res)
      if(records){
        this.setData({
          records:[...this.data.records,...records],
          pageNum: ++this.data.pageNum,
          pages: records.length,
          showData: true,
        })
        setTimeout(() => {
          this.setData({
            loadFinish: true
          })
        }, 300);
        resolve(records.length)
      }
      reject(false)
      
    })
  },
  toDetail({currentTarget:{dataset:{id}}}){
    wx.navigateTo({
      url: '/pages/goods/detail/index?id='+ id
    });
  },
  toSearch(){
    wx.redirectTo({url:'/pages/search/index'})
  },
  onClickLeft(){
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({key}) {
    this.setData({
      key,
      'requestObj': {
        fetchData: (refresh) => {
          refresh ? this.setData({
            pageNum: 1,
            records: []
          }) : ''
          return this.getData()
        },
      },
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      console.log('设置选中项 1')
      this.getTabBar().setData({
        active: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})